package com.example.chinamobileai.controller;


import com.example.chinamobileai.config.MyChatMemory;
import com.example.chinamobileai.domain.vo.MessageVO;
import com.example.chinamobileai.service.IChatHistoryService;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 会话记录
 */
@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {

    @Autowired
    private MyChatMemory myChatMemory;
    @Autowired
    private IChatHistoryService chatHistoryService;
    /**
     * 获取会话列表
     * @param type 会话类型
     * @return 会话列表
     */
    @GetMapping("/{type}")
    public List<String> getHistory(@PathVariable String type) {
        return chatHistoryService.getChatIds(type,null);
    }

    /**
     * 获取会话记录
     * @param type 会话类型
     * @param chatId 会话ID
     * @return 会话记录
     */
    @GetMapping("/{type}/{chatId}")
    public List<MessageVO> getHistory(@PathVariable String type, @PathVariable String chatId) {
        List<Message> messages = myChatMemory.get(chatId);
        return messages.stream().map(MessageVO::new).toList();
    }
}
